var conform = function(){
	var cf = {};
    
	cf.FIELD_TYPES = {
        'CONSTANT' : 1,
        'FORMULA'  : 2,
        'SET'      : 3        
    };
    
	cf.Field = function(p, parentForm){
		this.cf    = parentForm;
		this.name  = p.name;
		this.index = null;
		
		if(p.constant){
			this.type = cf.FIELD_TYPES.CONSTANT;
			this.cf[this.name] = p.constant;
			this._recalc = function(){};
		}else if(p.formula){
			this.type = cf.FIELD_TYPES.FORMULA;
			this._recalc = new Function(p.formula);
			this._recalc();
		}else if(p.set){
			this.type = cf.FIELD_TYPES.SET;
			this._recalc  = new Function('this.range = ' + p.set);
//			this.getRange = function(){return this.cf[this.name]};
			this._recalc()
			if(this.range[0] instanceof Object){
				this.cf[this.name] = this.range[0].from;	
			}else{
				this.cf[this.name] = this.range[0];
			}			
		}
    };
    
    cf.Field.prototype.set = function(val){
    	this.cf[this.name] = val;
    };
    
    cf.Form = function(){
//    	this._init();
    };
    
    cf.Form.prototype._init = function(){
    	if(this._oF){
	    	for(var p in this._oF){
    			delete this[p];
    		};    		
    	};
    	this._aF = [];
    	this._oF = {};    	
    };

    cf.Form.prototype.load = function(aF){
    	this._init();
		for(var i=0,l=aF.length;i<l;i++){
			this._addF(aF[i]);	
		};
    };
    
    cf.Form.prototype._addF = function(oF){
    	this[oF.name] = null;
    	var index = this._aF.length;
    	this._aF.push(new cf.Field(oF, this));
    	this._oF[oF.name] = this._aF[index];
    	
    	this._oF[oF.name].index = index;		
    };
    
    cf.Form.prototype.set = function(fieldName, val){
    	this[fieldName] = val;
    	this._recalc(this._oF[fieldName].index + 1);
    };
    
    cf.Form.prototype._recalc = function(index){
    	if(index == undefined)
    		index = 0;
    	
    	for(var i = index, l=this._aF.length; i<l; i++){
    		this._aF[i]._recalc();
    	}			
    };

/*    
    cf.Form.prototype.dump = function(){
    	for(var p in this){
    		console.log(p)
    	}
    };
*/
    return cf;
}();